// components/addshelf/addshelf.js
import { changeMyBook } from "../../utils/api.js";
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        bookId: {
            type: Number,
            value: 0
        },
        isAdd: {
            type: Number,
            value: 0
        }
    },

    /**
     * 组件的初始数据
     */
    data: {

    },

    /**
     * 组件的方法列表
     */
    methods: {
        changeShelf: function () {
            // console.log(this.data.bookId)
            var active = this.data.isAdd;
            changeMyBook([this.data.bookId], active, (res) => {
                if (active == 0) {
                    wx.showToast({
                        title: '添加成功',
                        success: "success"
                    });
                    this.setData({
                        isAdd: true
                    });
                    var temp = wx.getStorageSync("bookJoined") ? wx.getStorageSync("bookJoined") : [];
                    // console.log(temp)
                    if (temp.indexOf(this.data.bookId) == -1) {
                        temp.push(this.data.bookId);
                        console.log("已加入" + this.data.bookId);
                    }
                    wx.setStorageSync("bookJoined", temp)
                    // console.log(temp)
                } else {
                    wx.showToast({
                        title: '移出成功',
                        success: "success"
                    });
                    this.setData({
                        isAdd: false
                    });
                    var temp = wx.getStorageSync("bookJoined") ? wx.getStorageSync("bookJoined") : [];
                    temp.splice(temp.indexOf(this.data.bookId), 1);
                    console.log("已移除" + this.data.bookId);
                    wx.setStorageSync("bookJoined", temp)
                }
            })
        },
    }
})
